/**
 * 
 */
package com.aurora.base.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.EncoderException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qiniu.api.auth.AuthException;
import com.qiniu.api.auth.digest.Mac;
import com.qiniu.api.config.Config;
import com.qiniu.api.io.IoApi;
import com.qiniu.api.io.PutExtra;
import com.qiniu.api.io.PutRet;
import com.qiniu.api.rs.BatchCallRet;
import com.qiniu.api.rs.EntryPath;
import com.qiniu.api.rs.GetPolicy;
import com.qiniu.api.rs.PutPolicy;
import com.qiniu.api.rs.RSClient;
import com.qiniu.api.rs.URLUtils;

/**
 * 
 * <p>
 * 创建时间: 2015年3月19日 下午4:17:54
 * </p>
 * 
 * @author <a href="mailto:chaowen.zheng@tentcoo.com">wen</a>
 * @version V1.0
 * @since V1.0
 */
public class QiniuUtil {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(QiniuUtil.class);

	private static Mac mac;

	// public static String bucket = "tangguoyuan";
	public static String bucket = "used2image";

	// public static String qiniuDomain =
	// "http://7xi6yz.com2.z0.glb.qiniucdn.com/";
	
	public static String qiniuDomain = "http://oc8rk0kl0.bkt.clouddn.com/";

	public static int payMoneyDayOfMonth = 5;

	static {
		Config.ACCESS_KEY = "6L5otJWe3HA47HKx6wYRmpMHGYiu5LraAumEGZIb";
		Config.SECRET_KEY = "9KTpIl0cnLv2irCcq4AxDePzUrOe0CODdHbCOeTt";
		mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY);
	}

	/**
	 * 获得七牛token
	 * 
	 * @return
	 * @throws Exception
	 */
	public static String getUptoken(String bucket) throws Exception {
		// 请确保该bucket已经存在
		PutPolicy putPolicy = new PutPolicy(bucket);
		return putPolicy.token(mac);
	}

	/**
	 * 七牛上传图片
	 * 
	 * @param key
	 *            保存在服务器上的资源唯一标识（文件名）。static/o0/ac/ba/12/uuid.jpg
	 * @param file
	 *            上传文件
	 * @return
	 * @throws Exception
	 */
	public static boolean upload(String bucket, String key, File file)
			throws Exception {
		PutExtra extra = new PutExtra();
		PutRet ret = IoApi.putFile(getUptoken(bucket), key, file, extra);
		return ret.ok();
	}

	/**
	 * 下载图片
	 * 
	 * @param domain
	 *            域名
	 * @param key
	 *            文件标志（名字）
	 * @return
	 * @throws EncoderException
	 * @throws AuthException
	 */
	public static String download(String domain, String key)
			throws EncoderException, AuthException {
		String baseUrl = URLUtils.makeBaseUrl(domain, key);
		GetPolicy getPolicy = new GetPolicy();
		Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY);
		String downloadUrl = getPolicy.makeRequest(baseUrl, mac);
		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("download qiniu's url is:" + downloadUrl);
		}
		return downloadUrl;
	}

	/**
	 * 删除七牛服务器上面图片
	 * 
	 * @param bucket
	 * @param key
	 * @return
	 */
	public static boolean delete(String bucket, String key) {
		RSClient client = new RSClient(mac);
		return client.delete(bucket, key).ok();
	}

	/**
	 * 七牛多图片删除
	 * 
	 * @param bucket
	 *            七牛空间名
	 * @param keys
	 *            文件名字列表
	 * @return
	 */
	public static boolean batchDelete(String bucket, List<String> keys) {
		if (keys == null || keys.size() == 0) {
			return false;
		}
		RSClient rs = new RSClient(mac);
		List<EntryPath> entries = new ArrayList<EntryPath>();
		for (String key : keys) {
			EntryPath e = new EntryPath();
			e.bucket = bucket;
			e.key = key;
			entries.add(e);
		}
		BatchCallRet bret = rs.batchDelete(entries);
		return bret.ok();
	}

}
